package com.tansty.web.controller;

import com.tansty.common.utils.CommonUtil;
import com.tansty.pojo.dto.OrderDTO;
import com.tansty.pojo.vo.ResultVO;
import com.tansty.service.service.OrdersService;
import com.tansty.web.exception.Errors;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

/**
 * description:  <br>
 * date: 2021/8/27 13:19 <br>
 * author: ztz <br>
 * version: 1.0 <br>
 * @author Tansty
 */
@RestController
@RequestMapping("/order")
public class OrdersController {
    @Autowired
    OrdersService ordersService;
    @GetMapping("/all")
    public ResultVO getAll(){
        return CommonUtil.ajaxReturn(ordersService.getAll());
    }
    @PutMapping("/update")
    public ResultVO updateOrder(@RequestBody OrderDTO orderDTO){
        int code = ordersService.updateOrder(orderDTO);
        if (code== Errors.NOT_FOUND_PET.getCode()){
            return new ResultVO("请求出错",Errors.NOT_FOUND_PET.getCode(),Errors.NOT_FOUND_PET.getMsg());
        }else if(code== Errors.NOT_FOUND_USER.getCode()){
            return new ResultVO("请求出错",Errors.NOT_FOUND_USER.getCode(),Errors.NOT_FOUND_USER.getMsg());
        }
        return CommonUtil.ajaxReturn(code);
    }
    @PreAuthorize("hasAuthority('AddOrder')")
    @PostMapping("/add")
    public ResultVO addOrder(@RequestBody OrderDTO orderDTO){
        int code = ordersService.addOrder(orderDTO);
        if (code== Errors.NOT_FOUND_PET.getCode()){
            return new ResultVO("请求出错",Errors.NOT_FOUND_PET.getCode(),Errors.NOT_FOUND_PET.getMsg());
        }else if(code==Errors.PET_IS_BUY.getCode()){
            return new ResultVO("请求出错",Errors.PET_IS_BUY.getCode(),Errors.PET_IS_BUY.getMsg());
        }
        else if(code== Errors.NOT_FOUND_USER.getCode()){
            return new ResultVO("请求出错",Errors.NOT_FOUND_USER.getCode(),Errors.NOT_FOUND_USER.getMsg());
        }
        return CommonUtil.ajaxReturn(code);
    }
    @PreAuthorize("hasRole('ADMIN')")
    @DeleteMapping("/delete")
    public ResultVO deleteOrder(@RequestBody OrderDTO orderDTO){
        return CommonUtil.ajaxReturn(ordersService.deleteOrder(orderDTO));
    }

    @GetMapping("/line")
    public ResultVO getLineData(){
        return CommonUtil.ajaxReturn(ordersService.getLineData());
    }
}
